package mn.idax.exchange.otc.util;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.formula.functions.T;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

/**
 * @Auther: dison
 * @Date: 2018/8/3 12:00
 * @Description: excel导出工具类
 * 具体可见：https://www.jianshu.com/p/5d67fb720ece
 */
public class EasyPOIExcelExportUtil {

    /**
     *
     * 功能描述:
     * @param: list 数据源
     * @param: title 标题名称
     * @param: sheetName
     * @param: pojoClass
     * @param: fileName 导出的文件名称
     * @param: isCreateHeader
     * @auther: dison
     * @date: 2018/8/3 13:35
     */
    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, boolean isCreateHeader, HttpServletResponse response) throws IOException{
        ExportParams exportParams = new ExportParams(title, sheetName);
        exportParams.setCreateHeadRows(isCreateHeader);
        defaultExport(list, pojoClass, fileName, response, exportParams);

    }

    public static void exportExcel(List<?> list, String title, String sheetName, Class<?> pojoClass, String fileName, HttpServletResponse response) throws IOException{
        defaultExport(list, pojoClass, fileName, response, new ExportParams(title, sheetName));
    }

    public static void exportExcel(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException{
        defaultExport(list, fileName, response);
    }

    private static void defaultExport(List<?> list, Class<?> pojoClass, String fileName, HttpServletResponse response, ExportParams exportParams) throws IOException{
        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, pojoClass, list);
        if (workbook != null) {
            ;
        }
        downLoadExcel(fileName, response, workbook);
    }

    private static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setHeader("content-Type", "application/vnd.ms-excel");
        response.setHeader("Content-Disposition",
                "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
        workbook.write(response.getOutputStream());
    }

    private static void defaultExport(List<Map<String, Object>> list, String fileName, HttpServletResponse response) throws IOException{
        Workbook workbook = ExcelExportUtil.exportExcel(list, ExcelType.HSSF);
        if (workbook != null) {
            ;
        }
        downLoadExcel(fileName, response, workbook);
    }

    public static <T> List<T> importExcel(String filePath, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws NoSuchElementException,Exception{
        if (StringUtils.isBlank(filePath)) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = ExcelImportUtil.importExcel(new File(filePath), pojoClass, params);
        return list;
    }

    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows, Class<T> pojoClass) throws NoSuchElementException,Exception{
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        List<T> list = ExcelImportUtil.importExcel(file.getInputStream(), pojoClass, params);
        return list;
    }

    public static String getOrderExcelFileName(Date startTime, Date endTime){
        StringBuffer excelFileName = new StringBuffer();
        String ext = ".csv";
        excelFileName.append(getOrderExcelTitle());
        if (null != startTime && null != endTime){
            String startTimeStr = DateUtils.getDateToStr(startTime,"yyyyMMdd");
            String endTimeStr = DateUtils.getDateToStr(endTime,"yyyyMMdd");
            excelFileName.append("(");
            excelFileName.append(startTimeStr);
            excelFileName.append("~");
            excelFileName.append(endTimeStr);
            excelFileName.append(")");
        }
        excelFileName.append(ext);
        return excelFileName.toString();
    }

    public static String getOrderExcelTitle(){
        return "Orders";
    }
}
